<template>
    <div>
        <el-dialog
            :title="$t('common.edit')"
            :visible.sync="isVisibleRepayDialog"
            width="80%">
            <editItem v-for="item,index in dataList" :index="index" @delate="delate" :channelCodeList="channelCodeList"   :item="item" :appList="appList"/>
            <footer @click="addList"><i class="el-icon-plus"></i></footer>
            <span slot="footer" class="dialog-footer">
                <el-button @click="isVisibleRepayDialog = false">{{$t('common.cancel')}}</el-button>
                <el-button type="primary" @click="changeSubmit" >{{$t('common.confirm')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import editItem from './editItem'
export default {
    components:{
        editItem
    },
    props:{
        propData:{
            default:[]
        }
    },
    data(){
        return {
            isCheck:false,
            isVisibleRepayDialog:false,
            channelCodeList:[],
            appList:[],
            dataList:[]
        }
    },
    methods:{
        delate(index){
            this.dataList.splice(index, 1);
        },
        addList(){
            let obj = {
                code:null,
                companyCode:null,
                appNameList:[],
                isUse: null,
                isError:0
            }
            this.dataList.push(obj)
        },
        changeSubmit(){
            let isSub = true;
            let params = []
           
            for(let i=0;i<this.dataList.length;i++){
                let item = this.dataList[i];
                for(let j=0;j<this.dataList.length;j++){
                    let jtem = this.dataList[j]
                    if(i!=j&&item.code==jtem.code){
                        for(let p=0;p<item.appNameList.length;p++){
                            let nameItem = item.appNameList[p]
                            for(let k=0;k<jtem.appNameList.length;k++){
                                if(jtem.appNameList[k] == nameItem){
                                    item.isError = 1
                                    item.ErrMsg = '一个app中一个支付方式只能对应一个支付渠道'
                                    isSub = false
                                    console.warn(item)
                                    return
                                }
                            }
                            // if(isSub = false){
                            //     return
                            // }
                        }
                    }
                }
                let obj = {
                    code:item.code,
                    companyCode:item.companyCode,
                    appName:item.appNameList.join(','),
                    isUse: item.isUse
                }
                params.push(obj)
                if (item.appNameList.length<=0||item.code==null||item.companyCode==null){
                    item.isError = 1
                    isSub = false
                    return
                }
            }
            if (isSub&&params.length>0){
                this.$api.finance.productSetting.updateLoanChannels({req:params}).then(res=>{
                    const {status,data,error} = res;
                    if ('1' === status && error === '00000000') {
                        this.isVisibleRepayDialog = false;
                        this.$emit('addSuccess')
                    }
                    
                })}
        },
        async getAppList() {
            const params = {
                appListDomain: this.$storage.get('user').domain
            };
            const {
            status,
            data,
            error
            } = await this.$api.systemManage.getAppListdomain(params);
            if ('1' === status && error === '00000000') {
             this.appList = data || [];
            }
        },
        shoDialog(){
            let dataList = []
            this.propData.forEach(item=>{
                let obj = {
                    code:item.code,
                    companyCode:item.companyCode,
                    appNameList:item.appNameList,
                    isUse:item.isUse,
                    isError:0
                };
                dataList.push(obj)
            })
            this.dataList = dataList
            this.isVisibleRepayDialog = true
        },
        getLoanChannelCode(){
            this.$api.finance.productSetting.getLoanChannelCode({}).then((res) => {
                const {status, error,data} = res;
                if ('1' === status && error === '00000000') {
                    this.channelCodeList = data
                }
            });
        }
    },
    created(){
        
        this.getLoanChannelCode()
        this.getAppList()
    },
}
</script>
<style lang="scss">
 ul{
    display: block;
    li{
        display: block;
      
       
    }
 }
 footer{
    text-align: center;
    padding: 20px;
    box-sizing: border-box;
    font-size: 30px;
    color: #000;
    justify-content: center;
    border: 1px solid #ccc;
    cursor: pointer;
    margin-top: 20px;
 }
</style>